﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;

namespace MyDMS_ExtJS.Admin
{
    public partial class DepartmentEdit : System.Web.UI.Page
    {
        private int _Department_Code = 0;
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                _Department_Code = Convert.ToInt32(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                var sf = _myDmsEntity.Department.FirstOrDefault(s => s.Department_Code == _Department_Code);
                if (sf != null)
                {
                    tbName.Text = sf.Department_Name;
                    tbDesc.Text = sf.Department_Desc;
                }
            }
        }
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if (_Department_Code != 0)
            {
                var sf = _myDmsEntity.Department.FirstOrDefault(s => s.Department_Code == _Department_Code);
                if (sf != null)
                {
                    sf.Department_Name = tbName.Text;
                    sf.Department_Desc = tbDesc.Text;
                }
                _myDmsEntity.SaveChanges();
            }
            else
            {
                _myDmsEntity.Department.AddObject(new MyDMS.DAL.Department()
                {
                    Department_Name = tbName.Text,
                    Department_Desc = tbDesc.Text
                });
                _myDmsEntity.SaveChanges();
            }

            // 2. 关闭本窗体，然后刷新父窗体
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
    }
}